package com.yanqu.road.logic.activity.silkroad.refloat;

//2D的方形实例,有x和y坐标，有长宽，只有横向的速度,速度有方向0不动，1向左，2向右，x有移动范围

import com.yanqu.road.entity.activity.silkroad.config.SilkRoadSeaConfig;

public class SilkRoadSeaItem {
    private int itemId;
    private double x;
    private double y;
    private double width;
    private double height;
    private double speed;
    private int direction;
    private double maxMoveX;
    private double minMoveX;
    /**
     * 命中时钩子的坐标
     */
    private double hookX;
    private double hookY;

    private double returnX;
    private double returnY;

    public SilkRoadSeaItem(double returnX, double returnY) {
        this.returnX = returnX;
        this.returnY = returnY;
    }

    public SilkRoadSeaItem(SilkRoadSeaConfig config) {
        this.itemId = config.getId();
        this.x = config.getX();
        this.y = config.getY();
        this.width = config.getWidth();
        this.height = config.getHeight();
        this.speed = config.getSpeed();
        this.direction = config.getMoveType();

        if (direction == 1) {
            this.maxMoveX = this.x;
            this.minMoveX = this.x - config.getMoveRange();
        } else if (direction == 2) {
            this.maxMoveX = this.x + config.getMoveRange();
            this.minMoveX = this.x;
        } else {
            this.maxMoveX = this.x;
            this.minMoveX = this.x;
        }
    }

    public SilkRoadSeaItem(int itemId, double x, double y, double width, double height, double speed, int direction, double maxDistance) {
        this.itemId = itemId;
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
        this.speed = speed;
        this.direction = direction;

        if (direction == 1) {
            this.maxMoveX = this.x;
            this.minMoveX = this.x - maxDistance;
        } else if (direction == 2) {
            this.maxMoveX = this.x + maxDistance;
            this.minMoveX = this.x;
        } else {
            this.maxMoveX = this.x;
            this.minMoveX = this.x;
        }
    }

    public void move() {
        if (direction == 1) {
            x -= getMoveSpeed();
            if (x < minMoveX) {
                x = minMoveX + minMoveX - x;
                direction = 2;
            }
        } else if (direction == 2) {
            x += getMoveSpeed();
            if (x > maxMoveX) {
                x = maxMoveX - (x - maxMoveX);
                direction = 1;
            }
        }
    }

    /**
     * speed的单位是秒,移动按100ms算一次
     * @return
     */
    public double getMoveSpeed() {
        return speed/10;
    }

    public void moveByTime(long time) {
        if (direction == 1) {
            x -= speed * time / 1000;
        } else if (direction == 2) {
            x += speed * time / 1000;
        }

        while (x < minMoveX || x > maxMoveX) {
            if (x < minMoveX) {
                x = minMoveX + minMoveX - x;
                direction = 2;
            } else if (x > maxMoveX) {
                x = maxMoveX - (x - maxMoveX);
                direction = 1;
            }
        }
    }

    public double getX() {
        return x;
    }

    public double getY() {
        return y;
    }

    //x和y在左下角,判断是否被鱼钩击中
    public boolean isHit(SilkRoadHook hook) {
        double hookX = hook.getX();
        double hookY = hook.getY();
        double hookRadius = hook.getRadius();

        //过滤超过半径的坐标
        if (hookX < x - hookRadius || hookX > x + width + hookRadius || hookY < y - hookRadius || hookY > y + height + hookRadius) {
            return false;
        }

        //找到矩形内离圆心最近的点
        double nearX = hookX - Math.max(x, Math.min(hookX, x + width));
        double nearY = hookY - Math.max(y, Math.min(hookY, y + height));
        boolean isHit = (nearX * nearX + nearY * nearY) <= (hookRadius * hookRadius);
        if (isHit){
            this.hookX = hook.getX();
            this.hookY = hook.getY();
            this.returnX = hook.getReturnX();
            this.returnY = hook.getReturnY();
        }
        return isHit;
    }

    public int getItemId() {
        return itemId;
    }

    public int getDirection() {
        return direction;
    }

    public void setX(double x) {
        this.x = x;
    }

    public void setY(double y) {
        this.y = y;
    }

    public void setDirection(int direction) {
        this.direction = direction;
    }

    public double getHookX() {
        return hookX;
    }

    public double getHookY() {
        return hookY;
    }

    public double getReturnX() {
        return returnX;
    }

    public double getReturnY() {
        return returnY;
    }
}
